package leetcode.剑指offer.剑指II;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/9/25 14:43
 * @description：
 */
public class 剑指_II_002_二进制加法 {
    @Test
    public void test() {
        Solution solution = new Solution();
    }

    class Solution {
        public String addBinary(String a, String b) {
            StringBuilder builder = new StringBuilder();
            int factor = 0;
            int i = a.length() - 1, j = b.length() - 1;
            while (i >= 0 || j >= 0) {
                int m = i >= 0 ? a.charAt(i--) - '0' : 0;
                int n = j >= 0 ? b.charAt(j--) - '0' : 0;
                int ans = m + n + factor;
                factor = ans / 2;
                builder.append(ans % 2);
            }
            if (factor != 0) builder.append(factor);
            return builder.reverse().toString();
        }
    }
}
